import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {configParams} from '../../../storage-config';

@Injectable({
  providedIn: 'root'
})
export class CommodityInfoService {

  /**
   * 获取仓库商品列表
   */
  public get_URL = '/whGoods/list';
  /**
   * 删除仓库商品
   * @param httpClient
   */
  public del_URL = '/whGoods/remove';
  /**
   * 同步
   */
  public itemSynchronize_URL = '/whgoods/itemSynchronize';
  /**
   * 同步商品库存
   */
  public stockInventoryQuery_URL = '/whgoods/stockInventoryQuery';
  /**
   * 仓库商品分类下拉
   */
  public type_URL = '/whGoodsType/list/chart/';
  /**
   * 仓库商品 品牌下拉
   */
    // public brand_URL = "/brand/list";
  public brand_URL = '/whGoodBrand/list/page';

  /**
   * 仓库商品分类下拉（树形结构tree）
   */
  public typeTree_URL = '/whGoodsType/list/chart';
  /**
   * 商品详情
   */
  public commodityDetail = '/whGoods/details';

  // bom列表
  public bomList = '/whGoods/conditionQuery';
  // 添加商品
  commodityAddUrl = '/whGoods/add';
  // 编辑商品
  commodityEditUrl = '/whGoods/update';


  constructor(private httpClient: HttpClient) {
  }

  POST(url: any, body: any) {
    return this.httpClient.post(url, body, {headers: configParams.headers});
  }

  GET(url: any, params: any = {}) {
    return this.httpClient.get(url, {params, headers: configParams.headers});
  }

  /**
   * 仓库商品列表
   */
  public getList(json: any | null): Observable<any> {
    const jsons = {...json};
    if (jsons.dateFormat.length !== 0) {
      jsons.updateBeginTime = this.shiftDate(jsons.dateFormat[0]);
      jsons.updateEndTime = this.shiftDate(jsons.dateFormat[1]);
    }
    Reflect.deleteProperty(jsons, 'dateFormat');
    jsons.exsistBom = jsons.exsistBom ? 1 : '';
    jsons.exsistSN = jsons.exsistSN === null ? '' : jsons.exsistSN ? 0 : 1;
    jsons.exsistWarning = jsons.exsistWarning ? 1 : '';
    jsons.type = jsons.type.join(',') || '';
    return this.POST(this.get_URL, jsons);
  }

  /**
   * 删除仓库商品
   */
  public del(json: any | null): Observable<any> {
    return this.POST(this.del_URL, json);
  }

  /**
   * 同步
   */
  public itemSynchronize(): Observable<any> {
    return this.POST(this.itemSynchronize_URL, null);
  }

  /**
   * 同步商品库存
   */
  public stockInventoryQuery(): Observable<any> {
    return this.POST(this.stockInventoryQuery_URL, null);
  }

  /**
   * 仓库商品分类下拉
   */
  public getGoodsTypeList(id: any = 1): Observable<any> {
    // const url = `${this.type_URL}${id}`
    const url = `${this.typeTree_URL}`;
    return this.GET(url);
  }

  /**
   * 仓库商品 品牌下拉
   */
  public getBrandList(params: any): Observable<any> {
    return this.POST(this.brand_URL, params);
  }

  /**
   * 商品详情
   */
  getCommodityDetail(id: any): Observable<any> {
    return this.POST(this.commodityDetail, {id});
  }

  /**
   * BOM列表
   */
  getBomList(body: any): Observable<any> {
    return this.POST(this.bomList, body);
  }

  /**
   * 添加修改商品信息
   */
  commodityAdd(body: any): Observable<any> {
    const url = body?.id ? this.commodityEditUrl : this.commodityAddUrl;
    return this.POST(url, body);
  }

  /**
   * 时间格式转换 年月日
   * @param date
   * @returns
   */
  shiftDate(date: any) {
    let d = new Date(date);
    let str = d.getFullYear() + '-' + this.p((d.getMonth() + 1)) + '-' + this.p(d.getDate());
    return str;
  }

  /**
   * 补0
   * @param s
   */
  p(s: any) {
    return s < 10 ? '0' + s : s;
  }
}
